package com.beaven.model.model.api;

import com.beaven.model.Factory;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author: Beaven
 * time: 2017/5/18 15:43
 */

public class PushModel {
  // 退出登录
  public static final int ENTITY_TYPE_LOGOUT = -1;
  // 普通消息送达
  public static final int ENTITY_TYPE_MESSAGE = 200;
  // 好友的删除
  public static final int ENTITY_TYPE_DELETE_FRIEND = 1001;
  // 群的删除
  public static final int ENTITY_TYPE_DELETE_GROUP = 1002;

  // 好友添加
  public static final int ENTITY_TYPE_ADD_FRIEND = 2001;
  // 群添加
  public static final int ENTITY_TYPE_ADD_GROUP = 2002;

  private List<Entity> entities = new ArrayList<>();

  private PushModel(List<Entity> entities) {
    this.entities = entities;
  }

  public List<Entity> getEntities() {
    return entities;
  }

  public void setEntities(List<Entity> entities) {
    this.entities = entities;
  }


  public static PushModel decode(String json) {
    Gson gson = Factory.getGson();
    Type type = new TypeToken<List<Entity>>() {
    }.getType();

    try {
      List<Entity> entities = gson.fromJson(json, type);
      if (entities.size() > 0) return new PushModel(entities);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  public static class Entity {
    public Entity() {

    }

    // 消息类型
    public int type;
    // 消息实体
    public String content;
    // 消息生成时间
    public Date createAt;
  }

  @Override public String toString() {
    return "PushModel{" + "entities=" + entities + '}';
  }
}
